package org.cttic.visualization.visualization.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.client.FindIterable;
import com.mongodb.client.model.Filters;
import org.bson.Document;
import org.cttic.visualization.visualization.common.RouteIdentificationData;
import org.cttic.visualization.visualization.common.commonConf;
import org.cttic.visualization.visualization.common.trackQueryData;
import org.cttic.visualization.visualization.service.RouteIdentificationQuery;
import org.cttic.visualization.visualization.util.DataUtil;
import org.cttic.visualization.visualization.util.DateUtil;
import org.cttic.visualization.visualization.util.ParseJsonDataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shkstart
 * @create 2020-04-30 14:54
 * 2.3.3车辆途径识别点查询接口
 *
 */
@Service
public class RouteIdentificationQueryImpl implements RouteIdentificationQuery {

    @Autowired
    DataUtil dataUtil;

    @Autowired
    MongoTemplate mongoTemplate;
    @Override
    public String routeIdentificationQuery(String jsonData) {
        String provinceId=null;
        JSONObject json = ParseJsonDataUtil.getJsonData(jsonData);
        if(json.getString("provinceId")!=null){
            if(!json.getString("provinceId").contains("|")){
                 provinceId = json.getString("provinceId");
            }
        }
        RouteIdentificationData rtData = new RouteIdentificationData();
        //判断是否查询江西省
        if(provinceId.equals("36")) {

            String vehPlate = json.getString("vehPlate");
            String vehPlateColor = json.getString("vehPlateColor");
            String queryType = json.getString("queryType");

            String queryValue1 = json.getString("queryValue1");
            //
            if (queryType.equals("2")) {
                FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.mongoScross)
                        .find(Filters.and(Filters.eq("vehicleId", vehPlate)
                                , Filters.eq("vehicleIdColor", Integer.parseInt(vehPlateColor)),
                                Filters.eq("endLat", queryValue1)));


                rtData.setTotalReceivableAmt("***.**");
                rtData.setTotalDiscountAmt("**.**");
                rtData.setTotalBillAmt("***.**");
                rtData.setTotalArAmout("***");


                List<RouteIdentificationData.Identification> identifications = dataUtil.IdentificationQueryDataUtil(documents,rtData);
                rtData.setEndLatArray(identifications);



                ObjectMapper mapper = new ObjectMapper();
                try {
                    String value = mapper.writeValueAsString(rtData);
                    return value;
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
//
//        }else if (queryType.equals("1")){
//            String queryValue2 = Data.getString("queryValue2");     //结束时间
//            String startTime = DateUtil.TtoTime(queryValue1);
//            String endTime = DateUtil.TtoTime(queryValue2);
//            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.TrafficRecord_strip)
//                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
//                            Filters.eq("vehicleIdColor", vehPlateColor),
//                            Filters.lte("entryTime",endTime),
//                            Filters.gte("entryTime",startTime)));
//            List<trackQueryData.vehLocation> list = DataUtil.trackQueryDataUtil(documents);
//
//            trackQueryData trackQueryData = new trackQueryData();
//            trackQueryData.setVehLocationList(list);
//            ObjectMapper mapper = new ObjectMapper();
//            try {
//                String json = mapper.writeValueAsString(trackQueryData);
//                return json;
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//        }else if (queryType.equals("0")){
//            FindIterable<Document> documents = mongoTemplate.getDb().getCollection(commonConf.TrafficRecord_strip)
//                    .find(Filters.and(Filters.eq("vehicleId", vehPlate),
//                            Filters.eq("vehicleIdColor", vehPlateColor),
//                            Filters.gte("entryTime",queryValue1)));
//            List<trackQueryData.vehLocation> list = DataUtil.trackQueryDataUtil(documents);
//
//            trackQueryData trackQueryData = new trackQueryData();
//            trackQueryData.setVehLocationList(list);
//            ObjectMapper mapper = new ObjectMapper();
//            try {
//                String json = mapper.writeValueAsString(trackQueryData);
//                return json;
//            } catch (JsonProcessingException e) {
//                e.printStackTrace();
//            }
//        }


            }
        }
        System.out.println("*******查询省份不为江西或输入错误********");
        return null;
    }
}
